package com.yescall.mole.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.Table;
import com.yescall.mole.MoleGame;

public class AbstractScreen implements Screen {
	public static final int GAME_VIEWPORT_WIDTH = 400, GAME_VIEWPORT_HEIGHT = 240;
	
	protected final MoleGame game;
    protected final Stage stage;

    private BitmapFont font;
    protected SpriteBatch batch;
    private Skin skin;
    private Table table;
    
    
    public AbstractScreen(MoleGame game)
    {
        this.game = game;
        this.font = new BitmapFont();
        this.batch = new SpriteBatch();
        this.stage = new Stage( GAME_VIEWPORT_WIDTH, GAME_VIEWPORT_HEIGHT, true );
    }
    
    protected String getName()
    {
        return getClass().getSimpleName();
    }

    public BitmapFont getFont()
    {
        if( font == null ) {
            font = new BitmapFont();
        }
        return font;
    }

    public SpriteBatch getBatch()
    {
        if( batch == null ) {
            batch = new SpriteBatch();
        }
        return batch;
    }
    
    protected Table getTable()
    {
        if( table == null ) {
            table = new Table();
            table.debug();
            table.setFillParent( true );
            stage.addActor( table );
        }
        return table;
    }

    protected Skin getSkin()
    {
        if( skin == null ) {
            skin = new Skin( Gdx.files.internal( "skin/uiskin.json" ), Gdx.files.internal( "skin/uiskin.png" ) );
        }
        return skin;
    }
    
	@Override
    public void render(float delta)
    {
        // the following code clears the screen with the given RGB color (black)
        Gdx.gl.glClearColor( 1f, 1f, 1f, 0f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
        
        stage.act(delta);
        stage.draw();
        Table.drawDebug(stage);
    }

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		// resize the stage
        stage.setViewport( width, height, true );
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		 stage.dispose();
	     batch.dispose();
	     font.dispose();
	}

}
